//
//  YZLSelectTableViewCell0.m
//  Car
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLSelectTableViewCell0.h"
#import "UIImageView+WebCache.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@interface YZLSelectTableViewCell0 ()

@property (nonatomic, retain) UIImageView *picImage;
@property (nonatomic, retain) UILabel *titleLable;
@property (nonatomic, retain) UILabel *cateLable;
@property (nonatomic, retain) UIImageView *PinglunImage;
@property (nonatomic, retain) UILabel *countLable;

@property (nonatomic, assign) BOOL isNight;

@end
@implementation YZLSelectTableViewCell0

- (void)nightCheck {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *nightPath = [documentPath stringByAppendingPathComponent:@"night.txt"];
    
    NSError *error = nil;
    NSString *nightModel = [NSString stringWithContentsOfFile:nightPath encoding:NSUTF8StringEncoding error:&error];
    
    if ([nightModel isEqualToString:@"YES"]) {
        self.isNight = YES;
    } else {
        self.isNight = NO;
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        [self nightCheck];
        self.picImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.picImage];

        
        self.titleLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLable];
        self.titleLable.numberOfLines = 0;

        self.cateLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.cateLable];
        self.cateLable.textColor = [UIColor grayColor];
        self.cateLable.font = [UIFont systemFontOfSize:15];

        
        self.PinglunImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.PinglunImage];

        
        self.countLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.countLable];
        self.countLable.font = [UIFont systemFontOfSize:13];
        self.countLable.textColor = [UIColor grayColor];

        
//        if (self.isNight) {
//            self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
//            self.titleLable.textColor = [UIColor blackColor];
//            
//        } else {
//            self.contentView.backgroundColor = [UIColor colorWithRed:(CGFloat)34 / 255 green:(CGFloat)47 / 255 blue:(CGFloat)63 / 255 alpha:1.000];
//            self.titleLable.textColor = [UIColor lightGrayColor];
//        }
//        /* 夜间模式切换 */
//        [[NSNotificationCenter defaultCenter] addObserverForName:@"night" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
//            NSString *string = [note.userInfo objectForKey:@"night"];
//            if ([string isEqualToString:@"YES"]) {
//                self.contentView.backgroundColor = [UIColor colorWithRed:(CGFloat)34 / 255 green:(CGFloat)47 / 255 blue:(CGFloat)63 / 255 alpha:1.000];
//                self.titleLable.textColor = [UIColor lightGrayColor];
//                
//            } else {
//                self.contentView.backgroundColor = [UIColor whiteColor];
//                self.titleLable.textColor = [UIColor blackColor];
//                
//            }
//        }];
    }
    
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    self.picImage.frame = CGRectMake(WIDTH * 0.027, WIDTH * 0.027, WIDTH * 0.27, WIDTH * 0.21);
    self.titleLable.frame = CGRectMake(WIDTH * 0.31, WIDTH * 0.013, WIDTH * 0.667, WIDTH * 0.16);
    self.cateLable.frame = CGRectMake(WIDTH * 0.32, WIDTH * 0.181, WIDTH * 0.533, WIDTH * 0.053);
    self.PinglunImage.frame = CGRectMake(WIDTH * 0.745, WIDTH * 0.181, WIDTH * 0.053, WIDTH * 0.04);
    self.countLable.frame = CGRectMake(WIDTH * 0.83, WIDTH * 0.173, WIDTH * 0.1333, WIDTH * 0.053);
    
}

- (void)setDiscuss:(YZLDiscuss *)discuss {
    
    if (_discuss != discuss) {
        _discuss = discuss;
    }
    
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global, ^{
        NSURL *url =  [NSURL URLWithString: _discuss.postImage];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.picImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"D0"]];
        });
    });
    
    self.titleLable.text = _discuss.postTitle;
    self.cateLable.text = _discuss.forumName;
    self.PinglunImage.image = [UIImage imageNamed:@"7"];
    
    NSInteger i = [_discuss.viewCount integerValue];
    if (i > 9999) {
        float x = i / 10000;
        NSString *str = [[NSString alloc] initWithFormat:@"%.1f万", x];
        self.countLable.text = str;
    } else {
        
        self.countLable.text = [_discuss.viewCount stringValue];
    }
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
